#include <stdio.h>
#include <stdlib.h>
#include "sensor_data.pb-c.h"

// 模拟接收数据
void receive_data(uint8_t *buffer, size_t len) {
	Sensor__SensorData *msg;

	// 反序列化数据
	msg = sensor__sensor_data__unpack(NULL, len, buffer);
	if (msg == NULL) {
		fprintf(stderr, "Failed to unpack data\n");
		return;
	}
	// 打印接收到的数据
	printf("Received data:\n");
	printf("ID: %d\n", msg->id);
	printf("Name: %s\n", msg->name);
	printf("Temperature: %.2f\n", msg->temperature);

	// 释放反序列化后的数据
	sensor__sensor_data__free_unpacked(msg, NULL);
}

int main() {
	// 模拟接收到的数据
	uint8_t buffer[] = {
		0x08, 0x7b,  // id: 123
		0x12, 0x07, 0x53, 0x65, 0x6e, 0x73, 0x6f, 0x72, 0x41, // name: "SensorA"
		0x1d, 0x00, 0x00, 0xcc, 0x41};	 // temperature: 25.5
	size_t len = sizeof(buffer);
	
	receive_data(buffer, len);
	return 0;
}